<?php
namespace AuctionCore\Account;

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//


/**
* 帐号分组表
* 此组拥有权限列表，帐号加入此组，即可拥有权限
* @author  爱民
*/
class Group
{

    public  $auth_object = null; //授权对象
    private $model = null; 

    public function __construct($auth_object){
        
        $f_auth = new \AuctionCore\Account\FrontAuth();
            
        $this->auth_object = $f_auth;        
        $this->init_model();
    }
    
    /**
    * 启用分组
    * @return   void
    */
    public function enable($gid)
    {
         $where = "id='".$gid."'";        
         $ok = $this->update(array('status'=>'0'),$where);
    }
    
    /**
    * 禁用分组
    * @return   void
    */
    public function disable($gid)
    {
          $where = "id='".$gid."'";        
          $ok = $this->update(array('status'=>'1'),$where);
    }
    
    /**
    * 创建分组
    * 返回id
    * @param    Object $info    分组信息                              
    * @return   int
    */
    public function create($info)
    {
        $insertId = $this->model->add($info);
        return $insertId;
    }
    
    /**
    * 删除分组
    * @param    int $gid    分组id
    * @return   void
    */
    public function del($gid)
    {
         $ok = $this->model->delete($gid);
         return $ok;
    }
    
    /**
    * 更新分组
    * @param    Object $info    分组信息                              
    * @param    $where   条件信息 
    * @return   boolean
    */
    public function update($info,$where)
    {
         $ok = $this->model->where($where)->save($info);
         return $ok;
    }

    /**
    * @param gid 组id
    * @return array 组信息
    **/
    public function info($gid){
        $info = $this->model->where("id='".$gid."'")->select();
        if(count($info)>0)
            return $info[0];
        else
            return false;
    }

    /**
    * 分配权限规则给组
    * @param gid 组id
    * @param rule_id 规则id 或 rule_ids [ruleid1,ruleid2,ruleid3...]规则id列表
    *
    */
    public function add_rule($gid,$rule_ids){        
        $group_info = $this->model->where("id='{$gid}'")->select();        
        $rules = explode(',',$group_info[0]['rules']);
        if(gettype($rule_ids)=='array'){
            $rules = array_merge($rules,$rule_ids);
        }else{
            $rules[] = $rule_ids;
        }
        $sets = array('rules'=>implode(',', $rules)); 
        $ok = $this->update($sets,"id='".$gid."'");
        return $ok;
    }

    /**
    * 从组移除权限规则
    * @param gid 组id
    * @param rule_id 规则id 或 rule_ids [ruleid1,ruleid2,ruleid3...]规则id列表
    *
    */
    public function remove_rule($gid,$rule_ids){        
        $group_info = $this->model->where("id='{$gid}'")->select();        
        $rules = explode(',',$group_info[0]['rules']);

        if(gettype($rule_ids)!='array'){ 
            $rule_ids = array($rule_ids);
        }
        $rule_inter = array_intersect($rules,$rule_ids);
        $rule_diff = array_diff($rules,$rule_inter);
        $sets = array('rules'=>implode(',', $rule_diff));         
        $ok = $this->update($sets,"id='".$gid."'");
        return $ok;
    }

    private function init_model(){
        if(is_null($this->model)){
             $auth_db_cfg = $this->auth_object->get_config();              
             $this->model = M($auth_db_cfg["AUTH_GROUP"]);
        }
    }

}

?>